package grape.crm.rest.department.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.rest.mvc.BaseTreeController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.crm.rest.department.form.DepartmentCreateForm;
import grape.crm.rest.department.form.DepartmentUpdateForm;
import grape.crm.rest.department.form.DepartmentListPageForm;
import grape.crm.rest.department.vo.DepartmentVo;
import grape.crm.rest.department.mapper.DepartmentWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.crm.service.department.po.Department;
import grape.crm.service.department.api.IDepartmentService;
import java.util.List;
import grape.common.rest.vo.TreeNodeVo;
/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/crm/department")
@Api(tags = "部门相关接口")
public class DepartmentController extends BaseTreeController<DepartmentVo, Department> {

    @Autowired
    private DepartmentWebMapper currentWebMapper;
    @Autowired
    private IDepartmentService currentService;



     @ApiOperation("添加部门")
     @PreAuthorize("hasAuthority('department:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public DepartmentVo create(@RequestBody @Valid DepartmentCreateForm cf) {
         Department po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询部门")
     @PreAuthorize("hasAuthority('department:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public DepartmentVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除部门")
     @PreAuthorize("hasAuthority('department:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新部门")
     @PreAuthorize("hasAuthority('department:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public DepartmentVo update(@PathVariable String id,@RequestBody @Valid DepartmentUpdateForm uf) {
         Department po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("分页查询部门")
    @PreAuthorize("hasAuthority('department:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<DepartmentVo> listPage(DepartmentListPageForm listPageForm) {
         Department po = currentWebMapper.formToPo(listPageForm);
         return super.listPage(po,listPageForm);
     }

    @ApiOperation("部门树")
    @PreAuthorize("hasAuthority('department:single:tree')")
    @GetMapping("/tree")
    @ResponseStatus(HttpStatus.OK)
    public List<TreeNodeVo<DepartmentVo>> tree( String parentId) {
        return super.listToTreeNodeVo(super.getByParentId(parentId));
    }
}
